package at.jku.ase.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

/**
 * Created by IntelliJ IDEA.
 * User: stellit
 * Date: 12/20/11
 * Time: 12:53 PM
 * To change this template use File | Settings | File Templates.
 */
public final class FileUtil {
    private FileUtil() {
    }

    public final static byte[] load(String fileName) {
        try {
            FileInputStream fin = new FileInputStream(fileName);
            return load(fin);
        } catch (Exception e) {

            return new byte[0];
        }
    }

    public final static byte[] load(File file) {
        try {
            FileInputStream fin = new FileInputStream(file);
            return load(fin);
        } catch (Exception e) {
            return new byte[0];
        }
    }

    public final static byte[] load(final FileInputStream fin) {
        return new OpWithParameterThrowsException<byte[]>() {
            @Override
            public byte[] op() throws Exception {
                byte readBuf[] = new byte[512 * 1024];
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                int readCnt = fin.read(readBuf);
                while (0 < readCnt) {
                    bout.write(readBuf, 0, readCnt);
                    readCnt = fin.read(readBuf);
                }
                byte[] result = bout.toByteArray();
                fin.close();
                bout.close();
                return result;
            }
        }.execute();
    }


}